package org.zxl.app.manage;

import org.zxl.api.SpiDemoService;
import org.zxl.api.platform.service.PlatformService;
import org.zxl.api.vm.service.VmService;

import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;

/**
 * @author zhangxiaolong
 * @date 2025-02-17 16:27
 **/
public class ProviderManage {
    private static final Map<String, SpiDemoService> spiDemoServiceMap = new HashMap<>();

    private static final Map<String, PlatformService> platformServiceMap = new HashMap<>();

    private static final Map<String, VmService> vmServiceMap = new HashMap<>();

    public static void register() {
        ServiceLoader<SpiDemoService> loader = ServiceLoader.load(SpiDemoService.class);
        for (SpiDemoService service : loader) {
            String nameVersion = service.name() + "_" + service.version();
            spiDemoServiceMap.put(nameVersion, service);
        }
        System.out.println("register SpiDemoService");

        for (PlatformService service : ServiceLoader.load(PlatformService.class)) {
            String nameVersion = service.name() + "_" + service.version();
            platformServiceMap.put(nameVersion, service);
        }
        System.out.println("register PlatformService");

        for (VmService service : ServiceLoader.load(VmService.class)) {
            String nameVersion = service.name() + "_" + service.version();
            vmServiceMap.put(nameVersion, service);
        }
        System.out.println("register VmService");
    }

    public static SpiDemoService getSpiDemoService(String nameVersion) {
        return spiDemoServiceMap.get(nameVersion);
    }

    public static void printSpiDemoServiceVersion() {
        System.out.println("SpiDemoService 插件列表:");
        for (String serviceName : spiDemoServiceMap.keySet()) {
            System.out.println(serviceName);
        }
    }

    public static PlatformService getPlatformService(String nameVersion) {
        return platformServiceMap.get(nameVersion);
    }

    public static void printPlatformServiceVersion() {
        System.out.println("PlatformService 插件列表:");
        for (String serviceName : platformServiceMap.keySet()) {
            System.out.println(serviceName);
        }
    }

    public static VmService getVmService(String nameVersion) {
        return vmServiceMap.get(nameVersion);
    }

    public static void printVmServiceVersion() {
        System.out.println("VmService 插件列表:");
        for (String serviceName : vmServiceMap.keySet()) {
            System.out.println(serviceName);
        }
    }

}
